package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import com.atguigu.common.valid.AddGroups;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.service.BrandService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

import javax.validation.Valid;


/**
 * 品牌
 *
 * @author xiebo
 * @email xbcando@163.com
 * @date 2021-05-12 14:53:38
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:brand:save")
    public R save(@Validated(AddGroups.class) @RequestBody BrandEntity brand/*, BindingResult bindingResult*/){
/*        Map<String,String> map=new HashMap<>();
        if (bindingResult.hasErrors()){
            //若数据发生错误
            bindingResult.getFieldErrors().forEach((item)->{
                //获取错误信息
                String errorMessage = item.getDefaultMessage();

                //获取错误名
                String name = item.getField();
                map.put(name,errorMessage);
            });

            return R.error(400,"数据发生错误").put("data",map);
        }else {
            //成功执行
            brandService.save(brand);
            return R.ok();
        }*/

        brandService.save(brand);
        return R.ok();
    }
    //select a.dept_no,a.emp_no from dept_emp a,salaries b  where a.emp_no=b.emp_no group by a.dept_no order by a.dept_no ;
    //select a.dept_no,a.emp_no,max(b.salary) from dept_emp a,salaries b
    //where a.emp_no=b.emp_no group by a.dept_no order by a.dept_no ;
    // SELECT dept_emp.*,salaries.salary from dept_emp,salaries where dept_emp.emp_no=salaries.emp_no;
    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:brand:update")
    public R update(@RequestBody BrandEntity brand){
		brandService.updateById(brand);
       
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
